/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 */

#include "userConfig.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "event_groups.h"

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_PRIO         1
#define START_TASK_STACK_SIZE   256
TaskHandle_t    start_task_handler;

/* TASK1 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK1_PRIO         2
#define TASK1_STACK_SIZE   256
TaskHandle_t    task1_handler;

/* TASK2 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK2_PRIO         3
#define TASK2_STACK_SIZE   256
TaskHandle_t    task2_handler;


#define EVENTBIT_0  (1 << 0)
#define EVENTBIT_1  (1 << 1)


/* 任务一，发送任务通知值*/
static void task1(void * pvParameters)
{
    uint8_t ping_pang = 0;
    while(1)
    {
        if(ping_pang == 0)
        {
            ping_pang = 1;
            usart_transmit("将bit0位置1\r\n");
            xTaskNotify(task2_handler, EVENTBIT_0, eSetBits);
        }
        else
        {
            ping_pang = 0;
            usart_transmit("将bit1位置1\r\n");
            xTaskNotify(task2_handler, EVENTBIT_1, eSetBits);
        }
        vTaskDelay(100);
    }
}

/* 任务二，接收任务通知值 */
static void task2(void *pvParameters)
{
    uint32_t notify_val = 0, event_bit = 0;
    while(1)
    {
        xTaskNotifyWait(0, 0xFFFFFFFF, &notify_val, portMAX_DELAY);
        if(notify_val & EVENTBIT_0)
        {
            event_bit |= EVENTBIT_0;
        }
        if(notify_val & EVENTBIT_1)
        {
            event_bit |= EVENTBIT_1;
        }
        if(event_bit == (EVENTBIT_0|EVENTBIT_1))
        {
            usart_transmit("任务通知模拟事件标志组接收成功！！\r\n");
            event_bit = 0;
        }
    }
}

static void start_task(void * pvParameters)
{
    taskENTER_CRITICAL();
    xTaskCreate((TaskFunction_t         )   task1,
                (char *                 )   "task1",
                (configSTACK_DEPTH_TYPE )   TASK1_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK1_PRIO,
                (TaskHandle_t *         )   &task1_handler);

    xTaskCreate((TaskFunction_t         )   task2,
                (char *                 )   "task2",
                (configSTACK_DEPTH_TYPE )   TASK2_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK2_PRIO,
                (TaskHandle_t *         )   &task2_handler);

    vTaskDelete(NULL);

    taskEXIT_CRITICAL();
}

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void tasknotify_demo(void)
{
    xTaskCreate((TaskFunction_t         )   start_task,
                (char *                 )   "start_task",
                (configSTACK_DEPTH_TYPE )   START_TASK_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   START_TASK_PRIO,
                (TaskHandle_t *         )   &start_task_handler);
    vTaskStartScheduler();
}
